package net.sf.brutil.v0.light;



public class StringUtil {

	/**
	 * return the given string with a minimum size specified
	 * TODO: optimize
	 * size must be >3
	 */
	public static String toWidth(Object what,int size){
		return toWidth(what,size,' ',"\u2026");
	}
	
	public static String toMaxWidth(Object whatObject,int maxSize,String cutSignal){
		String what;
		if (whatObject==null) what="null";
		else what=whatObject.toString();
		
		// cut
		if (maxSize>0 && what.length()>maxSize && maxSize!=-1)
			what=what.substring(0,maxSize-cutSignal.length())+cutSignal;
		return what;
	}
	
	public static String toMinWidth(Object whatObject,int minSize){
		return toMinWidth(whatObject,minSize,' ',true);
	}
	public static String toMinWidth(Object whatObject,int minSize,char padding,boolean paddingOnRight){
		String what;
		if (whatObject==null) what="null";
		else what=whatObject.toString();
		
		// padding
		while (what.length()<minSize) {
		    if (paddingOnRight)
		        what=what+padding;
		    else
		        what=padding+what;
		}
		return what;
	}
	
	
	public static String toWidthRight(Object what,int size){
        return toWidth(what,size,size,' ',false,"\u2026");
    }
    
	public static String toWidthStraight(Object what,int size){
		return toWidth(what,size,' ',"");
	}

	public static String toWidth(Object whatObject,int size,char padding,String cutSignal){
	    return toWidth(whatObject, size, size, padding, true,cutSignal);
	}
	public static String toWidth(Object whatObject,int minSize,int maxSize,char padding,boolean paddingOnRight,String cutSignal){
		
		String what=toMinWidth(whatObject, minSize, padding, paddingOnRight);
		what=toMaxWidth(what, maxSize, cutSignal);		
		return what;
	}
	

	 
	 /**
	  * replace all CRLF, CR, or LF by CRLF
	  * @param what
	  * @return
	  */
	 public static String reformatCrLf(String what){
	        return reformatCrLf(what,"\r\n");
		 
	        
	    }
	 public static String reformatCrLf(String what,String properCrLf){
		 if (what==null) return null;
		 
			// convert all CRLF or CR only to LF
		    what=what.replaceAll("\r\n","\n");	
		    what=what.replaceAll("\n\r","\n");
		 	what=what.replaceAll("\r", "\n");
	        
	        
		 	if (!properCrLf.equals("\n")){
		        // convert all LF to CRLF
		        what=what.replaceAll("\n",properCrLf);
		 	}
	        
	        return what;
	 }
	

	public static String toTitleCase(String what){
		if (what.length()==1)
			what=what.toUpperCase();
		else if (what!=null && what.length()>1)
			return what.substring(0,1).toUpperCase()+what.substring(1);
		return what;
	}

	
	
    
	   public static int countOccurences(String s,String c){
			int cnt=0;
			for (int i=0;i<s.length();){
				String c2=s.substring(i);
				if (c2.startsWith(c)) {
					cnt++;
					i+=c.length();
				} else {
					i++;
				}
			}
			return cnt;
		}





	    
	    

	    public static String toWidth(Object whatObject,int minSize,int maxSize,char padding,String cutSignal,boolean rightPadding){
	        
	        String what;
	        if (whatObject==null) what="null";
	        else what=whatObject.toString();
	      //  System.err.println("l"+what.length());
	        while (what.length()<minSize && minSize>-1){
	            if (rightPadding)
	                what=what+padding;
	            else
	                what=padding+what;
	        }
	        if (maxSize>-1 && what.length()>maxSize)
	            what=what.substring(0,maxSize-cutSignal.length())+cutSignal;
	        return what;
	    }
	    
	    
	    public static Pair<Integer,Integer> getXYinText(int index,String text){
			int x=0,y=0;
			for(int i=0;i<index;i++){
				if (i>=text.length()) break;
				char c=text.charAt(i);
				if (c=='\n') {
					x=0;
					y++;
				} else if (c=='\r') {
					// ignore
				} else {
					x++;
				}
			}
			return new Pair<Integer, Integer>(x,y);
			
			
		}

		public static Pair<Integer,Integer> getTextSize(String s){
			   int x=0,y=0;
			   int maxx=-1;
			   
			   if (s==null) s="";
			   for (int i=0;i<s.length();i++){
				   char c=s.charAt(i);
				   if (c=='\r') {
					   // ignore
				   } else if (c=='\n') {
					   y++;
					   x=0;
				   } else {
					   
					   if (maxx<x)
						   maxx=x;
					   x++;
				   }
			   }
			   return new Pair<Integer, Integer>(maxx+1,y);
		   }

		

		
		public static String[] slice(String data,int sliceSize){
			
			int length=(data.length()/sliceSize)+1;
			String slices[]=new String[length];
			for(int i=0;i<length;i++){
				int startAt=i*sliceSize;
				int endAt=startAt+sliceSize;
				if (endAt>data.length()) endAt=data.length();
				slices[i]=data.substring(startAt, endAt);
				
			}
			return slices;
		}
		
		
		public static String cat(String ...data){
			StringBuffer retVal=new StringBuffer();
			for(String slice:data){
				retVal.append(slice);
			}
			return retVal.toString();
		}

	
		

		 
		 /**
		  * 
		  * @param sep the separator
		  * @param splitOnLeft if true, split using the first separator found. If false, split using the last separator
		  * @param key
		  * @return the key split in two. if no separator is found, the key is returned in the left side if splitOnLeft is true
		  * 
		  */
		 public static Pair<String,String> split(String sep,boolean splitOnLeft,String key){
				
				
				int sepIdx;
				
				if (splitOnLeft)
					sepIdx=key.indexOf(sep);
				else
					sepIdx=key.lastIndexOf(sep);
				
				String right="";
				String left="";
				if (sepIdx!=-1) {
					right=key.substring(sepIdx+1);
					left=key.substring(0,sepIdx);
				} else {
				    if (splitOnLeft) 
				        left=key;
				    else
				        right=key;
				}
				return new Pair<String,String>(left,right);		
			}

		

		
		 


			public static int[] string2rgb(String color){
				int[] retVal=new int[3];
				if (ObjectUtil.isEmptyOrBlank(color) || "transparent".equalsIgnoreCase(color)) return null;
				retVal[0]=Integer.parseInt(color.substring(1,3),16);
				retVal[1]=Integer.parseInt(color.substring(3,5),16);
				retVal[2]=Integer.parseInt(color.substring(5,7),16);
				return retVal;
			}
		    
		    
		    
		    
		    
			
		
		

}
